package com.myCommunity.ui;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myCommunity.dao.impl.CommentDaoImpl;
import com.myCommunity.dao.impl.ForumDaoImpl;
import com.myCommunity.dao.impl.TopicDaoImpl;
import com.myCommunity.entity.Comment;
import com.myCommunity.entity.Forum;
import com.myCommunity.entity.Topic;

public class CommentServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取父板块的id
		int referenceId = 0;
		
		if(request.getParameter("referenceId")!=null && !request.getParameter("referenceId").isEmpty())
			referenceId = Integer.parseInt(request.getParameter("referenceId"));
		request.setAttribute("referenceId", referenceId);
		
		Topic topic = new TopicDaoImpl().getTopicById(referenceId);
		request.setAttribute("topic",topic);
		
		Forum f = new ForumDaoImpl().getForumById(topic.getForumId());
		request.setAttribute("forum", f);
		
		int pageNum = 1;
		int pageSize = 4;
		if(request.getParameter("pageNum")!=null && !request.getParameter("pageNum").isEmpty())
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		if(pageNum>1)
			pageSize=5;
		
		int rows = new CommentDaoImpl().getListByReference(referenceId, 0, 0).size();
		int totalPage = (int)Math.ceil((double)rows/5);
		
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("pageNum", pageNum);
		
		List<Comment> list = new CommentDaoImpl().getListByReference(referenceId, pageNum, pageSize);
		
		request.setAttribute("comment", list);
		
		
		request.getRequestDispatcher("comment.jsp").forward(request, response);
	}

}
